package com.neuedu.carsystem.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.carsystem.model.Job;
import com.neuedu.carsystem.service.JobService;

@Controller
@RequestMapping("/findJob")
public class JobAction {
	@Autowired
	private JobService jobService;

	// 分页查询所有
	@RequestMapping("/findJobAll")
	public String findJobAll(HttpServletRequest request, HttpServletResponse response) {
		int page=Integer.parseInt(request.getParameter("page"));
		if(request.getParameter("page")==null) {
			page=1;
		}
		int next = page+1;
		int pre = page-1;
		int pageSize = 4;
		Map<String, Object> pageMap = new HashMap<String, Object>();
		int total = jobService.countJob();
		int pageCount=(int) Math.ceil(1.0*total/pageSize);
		if(page<1) {
			page=1;
		}
		pageMap.put("startPage", (page - 1) * pageSize);
		// 每页查询多少条记录
		pageMap.put("endPage", pageSize*page);
		// 进行分页查询
		List<Job> joblist = jobService.findJobAll(pageMap);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("job_List", joblist);
		request.setAttribute("total", total);
		request.setAttribute("nowpage", page);
		request.setAttribute("prepage", pre);
		request.setAttribute("nextpage", next);
		return "job_list";
	}
//		
	// 按ID查询
	@RequestMapping("/findByIdJob")
	public String findById(HttpServletRequest request, HttpServletResponse response) {
		int jno = Integer.parseInt(request.getParameter("id"));
		System.out.println("修改id为：" + jno);
		Job job = jobService.findById(jno);
		request.setAttribute("jobList", job);
		return "job_edit";
	}
	//添加
		@RequestMapping("/addJob")
		@ResponseBody
		public String addJob(HttpServletRequest request,HttpServletResponse response){
				int jno =Integer.parseInt(request.getParameter("jno"));
				String jname =	request.getParameter("jname");
				String jsite = request.getParameter("jsite");
				System.out.println(jno+","+jname+","+jsite);
				Job job = new Job(jno,jname,jsite);
				try {
					int ret =jobService.addJob(job);
				}catch (Exception e) {
				}
				return "{success:true}";
				
		}
		//修改
		@RequestMapping("/updateJob")
		public String updateJob(HttpServletRequest request,HttpServletResponse response){
				int jno =Integer.parseInt(request.getParameter("jno"));
				System.out.println("修改id为："+jno);
				String jname =request.getParameter("jname");
				String jsite =	request.getParameter("jsite");
				System.out.println(jno +" "+jname+" "+jsite);
				//int ret = carService.updateCar(carno);
				
			return null;
		}
		//删除
		@RequestMapping("/deleteJob")
		public String deleteJob(Integer id,HttpServletRequest request,HttpServletResponse response){
				int ret = jobService.deleteJob(id);
			return null;
		}
		

}
